<!--
  -    Copyright (c) 2018-2025, infomng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: infomng
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        :before-open="beforeOpen"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="handleDel">
        <template slot="menuLeft">
<!--          <el-button
            v-if="permissions.pfoc_publicprocessdevice_export"
            class="filter-item"
            plain
            type="primary"
            size="small"
            icon="el-icon-download"
            @click="exportExcel">导出
          </el-button>-->
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text"  icon="el-icon-edit"  size="small" plain  @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text"  icon="el-icon-delete"  size="small" plain  @click="handleDel(scope.row,scope.index)">删除</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, addObj, putObj, delObj } from '@/api/pfoc/publicprocessdevice'
import { tableOption } from '@/const/crud/pfoc/publicprocessdevice'
import {getPfocProcessList } from '@/api/pfoc/pfocprocess'
import { getDevice} from '@/api/hengdong/pfocinterface'//获取设备相关
import { getObj} from '@/api/pfoc/factorysettings' //获取当前工厂信息
import { mapGetters } from 'vuex'

export default {
  name: 'publicprocessdevice',
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      pfocProcessList:[],//工序集合
      organizationId:null,//组织ID
      organizationName:null,//组织名称
      deviceList:[],//设备集合
      tableOption:  {
          labelPosition: "left",
          labelSuffix: "：",
          labelWidth: 120,
          gutter: 0,
          menuBtn: true,
          align: "center",
          submitBtn: true,
          submitText: "提交",
          stripe: true,
          emptyBtn: true,
          emptyText: "清空",
          menuPosition: "center",
          border: true,
          index: true,
          indexLabel: "序号",
          searchMenuSpan:6,
          editBtn:false,
          delBtn:false,
          column: [
              {
                  type: "select",
                  label: "工序",
                  prop: "processId",
                  span: 12,
                  dicUrl: "/pfoc/pfocprocess/getPfocProcessList",
                  props: {
                      label: "processName",
                      value: "id"
                  },
                  dicMethod: "get",
                  change:({column,value})=>{
                      var t = this;
                      t.pfocProcessList.forEach(function (item, index) {
                            if(value == item.id){
                                t.$set(t.$refs['crud'].tableForm, 'processName', item.processName);
                            }
                      });
                  },
                  filterable:true,
                  rules: [
                      {
                          required: true,
                          message: "工序不能为空"
                      }
                  ],
              },
              {
                  type: "select",
                  label: "工序名称",
                  prop: "processName",
                  span: 12,
                  display:false,
                  hide:true,
                  showColumn:false,
              },
              {
                  type: "select",
                  label: "设备",
                  prop: "device",
                  span: 12,
                  dicData:[],
                  props: {
                      label: "device",
                      value: "device"
                  },
                  change:({column,value})=>{
                      var t = this;
                      t.deviceList.forEach(function (item, index) {
                          if(value == item.device){
                              t.$set(t.$refs['crud'].tableForm, 'deviceSn', item.deviceSn);
                              t.$set(t.$refs['crud'].tableForm, 'deviceName', item.deviceName);
                          }
                      });
                  },
                  filterable:true,
                  submit:false,
                  display:true,
                  hide:true,
                  showColumn:false,
                  rules: [
                      {
                          required: true,
                          message: "设备不能为空"
                      }
                  ],

              },
              {
                  type: "input",
                  label: "设备编码",
                  prop: "deviceSn",
                  span: 12,
                  disabled:true
              },
              {
                  type: "input",
                  label: "设备名称",
                  prop: "deviceName",
                  span: 12,
                  disabled:true
              }
          ]
      }
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.pfoc_publicprocessdevice_add, false),
        delBtn: this.vaildData(this.permissions.pfoc_publicprocessdevice_del, false),
        editBtn: this.vaildData(this.permissions.pfoc_publicprocessdevice_edit, false)
      }
    }
  },
  created() {
      //获取工序
      this.getPfocProcessListData();
      //获取当前工厂信息
      this.getFactoryInfo();
  },
  methods: {
    // 列表查询
    getList(page, params) {
      this.tableLoading = true
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    /**获取工序*/
    getPfocProcessListData(){
        getPfocProcessList().then(response => {
            this.pfocProcessList=response.data.data;
        })
    },
    /**获取工厂信息*/
    getFactoryInfo() {
        const factoryId=1;
        getObj(factoryId).then(response => {
            this.organizationId = response.data.data.menuId;//组织ID
            this.organizationName = response.data.data.organizationName;//组织名称
            //获取工厂设备集合
            this.getFactoryDeviceList();
        })
    },
    /**获取工厂设备集合*/
    getFactoryDeviceList(){
        var obj={
            position:this.organizationName,//设置组织查询条件
        }
        getDevice(obj).then(response => {
            this.deviceList = response.data.data;//设备集合
            this.deviceList.forEach(data=>{
                data.device = data.deviceName +"——"+data.deviceSn;
            })
            var column = this.findObject(this.tableOption.column, 'device');
            column.dicData =  this.deviceList;
        } )

    },
      //表单打开之前清空重置
      beforeOpen(done,type){
          if (["add"].includes(type)) {
              this.$nextTick(() => {
                  var form =  this.$refs['crud'].tableForm;
                  Object.keys(form).forEach((key) =>{
                      form[key] = null;
                  })
              })
          }
          done();
      },
    //默认编辑方法
    handleEdit:function(row,index){
        row.device =  row.deviceName +"——"+row.deviceSn;
        this.$refs.crud.rowEdit(row, index);
    },
    // 删除
    handleDel: function(row, index) {
      this.$confirm('是否确认删除ID为' + row.id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    // 更新
    handleUpdate: function(row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 保存
    handleSave: function(row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    // 当前页发生改变事件
    currentChange(current) {
      this.page.currentPage = current
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page)
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile('/pfoc/publicprocessdevice/export', this.searchForm, 'publicprocessdevice.xlsx')
    }
  }
}
</script>
